﻿Public Class ButtonsGridViewTemplate
    Implements ITemplate

    Private pTemplateType As DataControlRowType
    Private pAssistants As ServiceAssistant.OIItemListResult
    Private pLibelleDossier As String
    Private pLibelleFeminin As Boolean

    Sub New(templateType As DataControlRowType, assistants As ServiceAssistant.OIItemListResult, libelleDossier As String, libelleFeminin As Boolean)
        pTemplateType = templateType
        pAssistants = assistants
        pLibelleDossier = libelleDossier
        pLibelleFeminin = libelleFeminin
    End Sub

    Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
        Select Case pTemplateType

            Case DataControlRowType.Header
                Dim lbl As New Label()
                container.Controls.Add(lbl)

            Case DataControlRowType.DataRow
                Dim img As ImageButton

                Dim spacer As New Literal
                spacer.Text = " "

                ' Ajout du code en invisible
                Dim lblCode As New Label
                lblCode.Visible = False
                AddHandler lblCode.DataBinding, AddressOf Code_DataBinding
                container.Controls.Add(lblCode)

                ' Ajout d'un séparateur
                container.Controls.Add(spacer)

                ' Ajout du bouton d'édition
                img = New ImageButton
                img.AlternateText = "Modifier"
                img.CommandName = "Modifier"
                'img.CssClass
                img.Height = Unit.Pixel(20)
                img.ID = "btnModifier"
                img.ImageUrl = "~/Images/edit_20.png"
                'img.OnClientClick
                img.Style.Add("vertical-align", "middle")
                img.ToolTip = "Modifier"
                img.Width = Unit.Pixel(20)
                container.Controls.Add(img)

                ' Ajout d'un séparateur
                container.Controls.Add(spacer)

                ' Ajout du bouton de suppression
                img = New ImageButton
                img.AlternateText = "Supprimer"
                img.CommandName = "Supprimer"
                'img.CssClass
                img.Height = Unit.Pixel(20)
                img.ID = "btnSupprimer"
                img.ImageUrl = "~/Images/cancel_20.png"
                img.OnClientClick = "return confirm('Êtes-vous sûr de vouloir supprimer " & If(pLibelleFeminin, "cette ", "ce ") & pLibelleDossier & " ?');"
                img.Style.Add("vertical-align", "middle")
                img.ToolTip = "Supprimer"
                img.Width = Unit.Pixel(20)
                container.Controls.Add(img)

                ' Pour l'instant, on masque les assistants, car ça prend trop de place de les afficher sous la forme TextButton

                ' Ajout des assistants
                'For Each ass As ServiceAssistant.OIItem In pAssistants.Liste
                '    ' Ajout d'un séparateur
                '    container.Controls.Add(spacer)

                '    ' Ajout d'un bouton
                '    Dim btn As New Button
                '    btn.CommandArgument = ass.Code
                '    btn.CommandName = "Assistant"
                '    btn.CssClass = "btn"
                '    btn.ID = "btnAss" & ass.Code
                '    btn.OnClientClick = "return confirm('Êtes-vous sûr de vouloir exécuter cette action ?');"
                '    btn.Style.Add("height", "24px")
                '    btn.Text = ass.Libelle
                '    container.Controls.Add(btn)
                'Next

        End Select

    End Sub

    Private Sub Code_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
        Dim l As Label = CType(sender, Label)
        Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)
        l.Text = DataBinder.Eval(row.DataItem, "CodeDossier").ToString()
    End Sub
End Class
